<?php

use yii\helpers\Html;
use kartik\grid\GridView;
use johnitvn\ajaxcrud\CrudAsset;
use yii\bootstrap\Modal;
use yii\widgets\Pjax;

/* @var $this yii\web\View */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = 'Users';
$this->params['breadcrumbs'][] = $this->title;
CrudAsset::register($this);
?>
<div class="user-index">

    <h1><?= Html::encode($this->title) ?></h1>

    <p>
        <?= Html::a('Create User', ['create'], ['class' => 'btn btn-success']) ?>
    </p>
    <?php Pjax::begin(['id'=>'datalist']);?>
    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'columns' => [
            'id',
            'username',
            //'auth_key',
            //'password_hash',
            //'password_reset_token',
             'email:email',
             'status',
            [
                'attribute' => 'created_at',
                'label' => 'Created Time',
                'format'=>'raw',
                'value' => function ($model){
                    return date("Y-m-d H:i:s",$model->created_at);
                }
            ],
            [
                'class' => 'kartik\grid\ActionColumn',
                'template' => '{view}{update}{delete}',
                'buttons' => [
                    'view' => function ($url, $model) {
                        return Html::a('<span class="glyphicon glyphicon-eye-open"></span>View', $url, [
                            'title' => Yii::t('yii', 'View'),
                            'class' => 'btn btn-default btn-sm'
                        ]);
                    },
                    'update' => function ($url, $model) {
                        return Html::a('<span class="glyphicon glyphicon-pencil"></span>Update', ['update', 'id' => $model->id], [
                            'title' => Yii::t('yii', 'Add User'),
                            'class' => 'btn btn-default btn-sm',
                        ]);
                    },
                    'delete' => function ($url, $model) {
                        return Html::a('<span class=" glyphicon glyphicon-remove"></span>Delete', ['/user/delete-user','id' => $model->id], [
                            'title' => Yii::t('yii', 'Update'),
                            'class' => 'btn btn-default btn-sm',
                            'data' => [
                                'confirm' => 'Are you sure you want to delete this item?',
                                'method' => 'post',
                            ],
                        ]);
                    },
                    'assignment-team' => function ($url, $model) {
                        return html::button("Assignment Team",['title' => Yii::t('yii', 'assignment team'),'class' => 'btn btn-default btn-sm assignment','data-url' => $url]);
                    }
                ]
            ],
        ],
    ]); ?>
    <?php Pjax::end();?>
</div>
<?php Modal::begin([
    "id" => "ajaxCrudModal",
    "options" => ["tabindex" => ""],
    "footer" => "",
]) ?>
<?php Modal::end(); ?>

<script type="text/javascript">
    $(".assignment").click(function () {
        if(confirm("surl？")){
            $(this).attr('disabled','desabled');
            var url = $(this).data('url');
            $.ajax({
                url:url,
                type:"get",
                dataType:"json",
                success:function (ret) {
                    if( ret.ret ){
                        alert("success");
                    }else{
                        alert("failed");
                    }
                }
            });
        }
    });
</script>
